#include "PrintDatabase.h"

PrintDatabase::PrintDatabase()
{
    printer = new QPrinter();

    db = 0;
}

void PrintDatabase::init()
{
    //init is executed only when printer is choosen
    QRect pR; //printer rect
    pR = printer->pageRect();

    // t will be 3% of (pageH+pageW)/2
    t = 0.03 * (pR.width() + pR.height()) / 2;

    //rows on Page
    rN = 40;

    //description rect
    int dH = 0.05 * pR.height(); //height of description
    int dW = pR.width() - 2 * t;
    desR.setTopLeft( QPoint( t, t ) );
    desR.setBottomRight( QPoint( t + dW, t + dH ) );

    //table rect
    int tH = pR.height() - 3 * t - dH;
    int tW = dW;
    tR.setTopLeft( QPoint( t, 2 * t + dH ) );
    tR.setBottomRight( QPoint ( pR.width() - t, pR.height() - t ) );

    //number of rows
    n = db->GetItemsCount();
    //page number
    pN = n / rN;
    if ( n % rN != 0 ) pN++;

    //cell height
    hc = tR.height() / rN;

    // sum of column width in db
    int sum = 0;
    for (int i=0; i<db->GetFieldNumber(); i++)
        sum += db->GetColumnWidth( i );

    //calculating column width
    int width = 0;
    for (int i=0; i<db->GetFieldNumber(); i++){
        width = ((double)db->GetColumnWidth(i) / (double)sum) * tR.width();
        w.push_back( width );
    }
}

void PrintDatabase::drawDescription( QPainter *painter )
{
    //QLocale russian( QLocale::Russian, QLocale::RussianFederation );
    //QLocale egyptian(QLocale::Arabic, QLocale::Egypt);
    //QLocale::setDefault( russian );
    painter->drawText( desR.x(), desR.y(), db->getName() + QString("   ") + QDate::currentDate().toString() );
}

void PrintDatabase::drawTable()
{
    QPainter painter( printer );
    painter.begin( printer );

    painter.setPen( QPen( QBrush(Qt::black), 2 ) );
    //painter.setFont( QFont("Vernada", 12 ) );

    drawDescription( &painter );

    int sum = 0;
    int rows2print;

    for (int page=0; page<pN; page++){

        if (page!=0) printer->newPage();

        if (page == pN-1) rows2print = n % rN; else rows2print = rN;
        //rows2print++ because need to print header row;
        rows2print++;

        //Drawing table measures
        //drawing vertical
        int sum = 0;
        painter.drawLine( tR.x(), tR.y(), tR.x(), tR.y() + hc*rows2print );
        for (int i=0; i<db->GetFieldNumber(); i++){
            //rows2print+1 because need to print header row;
            sum += w[i];
            painter.drawLine( tR.x() + sum, tR.y(), tR.x() + sum, tR.y() + hc*rows2print );
        }

        //horizontal lines
        painter.drawLine( tR.topLeft(), tR.topRight() );
        for (int i=0; i<rows2print; i++){
            painter.drawLine( tR.x(), tR.y() + (i+1)*hc, tR.x() + tR.width(), tR.y() + (i+1)*hc );
        }

        //print text
        //print headers
        for (int i=0; i<db->GetFieldNumber(); i++)
            painter.drawText( getCellRect( i, 0 ), db->GetColumnHeader( i ), QTextOption(Qt::AlignCenter) );

        //print items
        ItemType item;
        for (int i=1; i<rows2print; i++){
            db->GetItem(page*rN + i-1, item );
            for (int j=0; j<db->GetFieldNumber(); j++)
                painter.drawText( getCellRect( j, i ), item.Get( j ), QTextOption(Qt::AlignCenter) );
        }
    }

     painter.end();
}



QRect PrintDatabase::getCellRect( int x, int y )
{
    int x1, y1;
    int sum = 0;

    for (int i=0; i<x; i++)
        sum += w[i];

    x1 = tR.x() + sum;

    y1 = tR.y() + y*hc;

    return QRect( x1, y1, w[x], hc );
}

void PrintDatabase::setDB( DataBase *db )
{
    this->db = db;
}

void PrintDatabase::Print()
{

    if (db == 0) return;

    QPrintDialog dialog(printer);

    if (dialog.exec()){

        init();
        drawTable();

        /*QPainter painter( printer );
        painter.begin( printer );

        painter.setPen( QPen( QBrush(Qt::black), 3 ) );

        printerRect = printer->pageRect();
        tableRect = QRect( (pageInterval + tableInterval) * printerRect.width(),
                           (pageInterval + descrHeight + tableInterval) * printerRect.height(),
                           tableWidth * printerRect.width(),
                           tableHeight * printerRect.height());

        QVector<int> columnWidth;

        int sum = 0;
        for (int i=0; i<db->GetFieldNumber(); i++)
            sum += db->GetColumnWidth( i );

        int w;
        QRect cell;
        cell.setX( 0 ); cell.setY( 0 ); cell.setHeight( cellHeight * printer->pageRect().height() );

        for (int i=0; i<db->GetFieldNumber(); i++){
            w = ((double)db->GetColumnWidth( i ) / (double)sum) * tableRect.width();

            cell.setWidth( w );
            cellRect.push_back( cell );
            columnWidth.push_back( w );
        }

        //Horisontal lines
        for (int i=0; i<=db->GetItemsCount() + 1; i++){
            painter.drawLine( tableRect.left(),
                              tableRect.top() + i*tableInterval*printerRect.height(),
                              tableRect.left() + tableRect.width(),
                              tableRect.top() + i*tableInterval*printerRect.height() );
        }
        //Vertical lines
        painter.drawLine(tableRect.x(), tableRect.y(), tableRect.x(),
                         tableRect.y() + printerRect.height() * cellHeight * db->GetItemsCount() );

        sum = 0;
        for (int i=0; i<db->GetFieldNumber(); i++){
            sum += columnWidth[i];
            painter.drawLine( tableRect.x() + sum, tableRect.y(), tableRect.x() + sum,
                              tableRect.y() + printerRect.height() * cellHeight * db->GetItemsCount() );
        }

        //Printing headers
        for (int i=0; i<fieldCount; i++)
            painter.drawText( getCellRect( i, 0 ), db->GetColumnHeader( i ),
                              QTextOption(Qt::AlignHCenter));

        painter.end();*/
    }
}
